<?php

namespace app\models;

use Yii;
use app\models\DhCommonmodel;
use yii\db\Query;

/**
 * This is the model class for table "dh_nodes".
 *
 * @property integer $NodeID
 * @property string $NodeIdentifier
 * @property integer $NodeType
 * @property integer $ParentID
 * @property string $ParentPath
 * @property integer $Depth
 * @property integer $RootID
 * @property integer $Child
 * @property string $arrChildID
 * @property integer $PrevID
 * @property integer $NextID
 * @property integer $OrderID
 * @property string $NodeDir
 * @property string $ParentDir
 * @property string $NodeName
 * @property string $Tips
 * @property string $Description
 * @property string $NodePicUrl
 * @property string $Meta_Keywords
 * @property string $Meta_Description
 * @property integer $ShowOnMenu
 * @property integer $ShowOnPath
 * @property integer $ShowOnMap
 * @property integer $ShowOnList_Index
 * @property integer $ShowOnList_Parent
 * @property integer $PurviewType
 * @property string $Creater
 * @property integer $InheritPurviewFromParent
 * @property integer $WorkFlowID
 * @property integer $HitsOfHot
 * @property integer $LeastOfEliteLevel
 * @property integer $OpenType
 * @property integer $ItemCount
 * @property integer $ItemChecked
 * @property integer $CommentCount
 * @property string $Custom_Content
 * @property integer $IsCreateListPage
 * @property integer $IsCreateContentPage
 * @property integer $AutoCreateHtmlType
 * @property string $ContentPageHtmlRule
 * @property string $ListPageHtmlRule
 * @property string $ItemAspxFileName
 * @property string $RelateNode
 * @property string $RelateSpecial
 * @property string $DefaultTemplateFile
 * @property string $ContainChildTemplateFile
 * @property integer $ItemOpenType
 * @property integer $ItemListOrderType
 * @property integer $ItemPageSize
 * @property string $UpLoadDirRule
 * @property string $LinkUrl
 * @property string $Settings
 * @property string $IPLock
 * @property string $ListPagePostFix
 * @property integer $ListPageSavePathType
 * @property integer $NeedCreateHtml
 * @property string $CultureName
 * @property integer $EnableSubDomain
 * @property string $SubDomain
 * @property integer $ConfigType
 * @property integer $IncrementalUpdatePages
 * @property integer $EnablePhone
 * @property integer $EnablePad
 */
class DhNodes extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'dh_nodes';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['NodeType', 'ParentID', 'Depth', 'RootID', 'Child', 'PrevID', 'NextID', 'OrderID', 'ShowOnMenu', 'ShowOnPath', 'ShowOnMap', 'ShowOnList_Index', 'ShowOnList_Parent', 'PurviewType', 'InheritPurviewFromParent', 'WorkFlowID', 'HitsOfHot', 'LeastOfEliteLevel', 'OpenType', 'ItemCount', 'ItemChecked', 'CommentCount', 'IsCreateListPage', 'IsCreateContentPage', 'AutoCreateHtmlType', 'ItemOpenType', 'ItemListOrderType', 'ItemPageSize', 'ListPageSavePathType', 'NeedCreateHtml', 'EnableSubDomain', 'ConfigType', 'IncrementalUpdatePages', 'EnablePhone', 'EnablePad'], 'integer'],
            [['ParentID', 'NodeName'], 'required'],
            [['ParentPath', 'arrChildID', 'ParentDir', 'Description', 'Custom_Content', 'RelateNode', 'RelateSpecial', 'Settings', 'IPLock'], 'string'],
            [['NodeIdentifier', 'NodeDir', 'NodeName', 'Creater', 'ItemAspxFileName', 'ListPagePostFix'], 'string', 'max' => 50],
            [['Tips', 'NodePicUrl', 'Meta_Keywords', 'Meta_Description', 'ContentPageHtmlRule', 'ListPageHtmlRule', 'DefaultTemplateFile', 'ContainChildTemplateFile', 'UpLoadDirRule', 'LinkUrl', 'SubDomain'], 'string', 'max' => 255],
            [['CultureName'], 'string', 'max' => 10]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'NodeID' => '节点ID',
            'NodeIdentifier' => '节点标识符，用于前台调用时可以直接用标识符取代ID。',
            'NodeType' => '节点类型。1为容器栏目，3为单页节点，4为外部链接',
            'ParentID' => '父节点ID，根节点的值为0',
            'ParentPath' => '父路径，根节点的值为0，子节点的值为：0,1,6,76',
            'Depth' => '节点树的深度，根节点的值为0，子节点的值为该节点所在的层数',
            'RootID' => '根节点排序ID。根节点使用RootID进行排序，同时子节点与根节点的RootID相等。',
            'Child' => '子节点数',
            'arrChildID' => '所有子节点的ID数组',
            'PrevID' => '同级节点的上一个节点ID',
            'NextID' => '同级节点的下一个节点ID',
            'OrderID' => '排序ID',
            'NodeDir' => '节点目录，只能是英文字母和数字，并且要以字母开头。',
            'ParentDir' => '父目录，系统自动根据树形目录算出来的目录路径',
            'NodeName' => '节点名称',
            'Tips' => '节点提示，不支持HTML',
            'Description' => '节点说明，支持HTML',
            'NodePicUrl' => '图片地址',
            'Meta_Keywords' => '针对搜索引擎的关键字',
            'Meta_Description' => '针对搜索引擎的说明',
            'ShowOnMenu' => '是否在顶部菜单处显示',
            'ShowOnPath' => '是否位置导航处显示',
            'ShowOnMap' => '是否在网站地图（栏目导航）处显示',
            'ShowOnList_Index' => '是否在首页的分类列表处显示',
            'ShowOnList_Parent' => '是否在父栏目的分类列表处显示',
            'PurviewType' => '栏目权限。0--开放栏目  1--半开放栏目  2--认证栏目',
            'Creater' => '栏目创建者',
            'InheritPurviewFromParent' => '从父栏目继承权限设置。',
            'WorkFlowID' => '工作流程ID',
            'HitsOfHot' => '本栏目热点的点击数最小值',
            'LeastOfEliteLevel' => '本栏目推荐的',
            'OpenType' => '此节点的打开方式( 0--原窗口  1--新窗口)',
            'ItemCount' => '项目数',
            'ItemChecked' => '审核项目数',
            'CommentCount' => '评论数',
            'Custom_Content' => '自设内容',
            'IsCreateListPage' => '列表页生成方式 0不生成，1生成',
            'IsCreateContentPage' => '内容页生成方式 0不生成，1生成',
            'AutoCreateHtmlType' => '自动生成Html类型',
            'ContentPageHtmlRule' => '内容页的文件名规则',
            'ListPageHtmlRule' => '列表页的文件名规则',
            'ItemAspxFileName' => '动态方式的内容页文件名',
            'RelateNode' => '关联节点',
            'RelateSpecial' => '关联专题',
            'DefaultTemplateFile' => '此节点的默认首页模板',
            'ContainChildTemplateFile' => '列表页模板',
            'ItemOpenType' => '此节点下项目的打开方式 ( 0--原窗口  1--新窗口)',
            'ItemListOrderType' => '此节点下项目列表的排序方式',
            'ItemPageSize' => '节点下内容每页分页数',
            'UpLoadDirRule' => '上传文件路径规则',
            'LinkUrl' => '外部链接地址',
            'Settings' => '节点设置，使用XML保存配置',
            'IPLock' => '节点IP设置，使用XML保存配置',
            'ListPagePostFix' => '列表页后缀',
            'ListPageSavePathType' => '列表页生成类型',
            'NeedCreateHtml' => '节点下是否有需要生成HTML的内容',
            'CultureName' => '文化名(语言种类)',
            'EnableSubDomain' => '是否开启子域名',
            'SubDomain' => '子域名',
            'ConfigType' => '节点配置类型',
            'IncrementalUpdatePages' => '增量更新页数',
            'EnablePhone' => 'Enable Phone',
            'EnablePad' => 'Enable Pad',
        ];
    }
    
    public static function getNodes(){
        
        $parList = DhNodes::find()->where(['status'=>1,'ParentID'=>0])->orderBy("OrderID DESC")->asArray()->all();
        return $parList;
    }
    
    public static function getNodes1(){
        
        $chrList = DhNodes::find()->where(['status'=>1,'ShowOnPath'=>1])->orderBy("OrderID DESC")->asArray()->all();
        return $chrList;
    }
    
    
    public static function getParentId($nodeid){
       
        $contlist['list'] = DhNodes::getList($nodeid);
           
   
        return $contlist;
    }
    
    
    public static function GetNews($id){
        $category = '';
        $condition = '';
        $list = DhNodes::findAll(["ParentID"=>$id,"status"=>1]);
        foreach ($list as $value) {
            $category .= $value['NodeID'] . ',';
        }
        $category = substr($category, 0, -1);
        $condition = $category;
        $condition = 'NodeID in(' . $condition . ')';
        $newsList = DhCommonmodel::find()->where("1=1 and ".$condition)->andWhere(["Status"=>99])->orderBy("stick DESC,(case  when  Priority IS NULL  then 99999999 else Priority end),Priority ASC,UpdateTime DESC")->limit(8)->offset(0)->asArray()->all();
        return $newsList;
    }


    public static function getChildNodes($nodeid){
        
        $Nodes = DhNodes::find()->where(['ParentID'=>0,'ShowOnPath'=>1])->asArray()->all();
        return $Nodes;
    }
    
    public static function getList($childid){
        
        $List = DhCommonmodel::find()->where(['NodeID'=>$childid,'Status'=>99])->orderBy("stick DESC,(case  when  Priority IS NULL  then 99999999 else Priority end),Priority ASC,UpdateTime DESC")->limit(8)->offset(0)->asArray()->all();
        return $List;
    }
    
    public static function getName($id){
        
        $Nodes = DhNodes::findOne(['NodeID'=>$id]);
        return $Nodes->NodeName;
    }
    
    
    public static function getNodeID($nodeid){
        
         $Nodes = DhNodes::findOne(['NodeID'=>$nodeid]);
        if($Nodes->ParentID == 0){
            $child = DhNodes::findAll(['ParentID'=>$Nodes->NodeID]);
            if(empty($child)){
                $contlist['id1'] = $Nodes->NodeID;
                $contlist['id'] = $nodeid;
                $contlist['ids'] = $Nodes->NodeID;
            }else{
                $list = $child[0];
                $contlist['id1'] = $list->NodeID;
                $contlist['id'] = $nodeid;
                $contlist['ids'] = $list->NodeID;
            }
            
//            print_r($list->NodeID);die();
        }else{
            $cont = DhNodes::getList($nodeid);
            if(empty($cont)){
                $child = DhNodes::findAll(['ParentID'=>$nodeid]);
                $list = $child[0];
                $contlist['id1'] = $list->NodeID;
//                print_r($contlist);die();
            }else{
                $contlist['id1'] = $Nodes->NodeID;
            }
            $contlist['id'] = $Nodes->ParentID;
            $contlist['ids'] = $Nodes->NodeID;
        }
        return $contlist;
    }
   
    
    public static function getListone($childid){
        
        $List = DhCommonmodel::find()->where(['NodeID'=>$childid,'Status'=>99])->orderBy("stick DESC,(case  when  Priority IS NULL  then 99999999 else Priority end),Priority ASC,UpdateTime DESC")->limit(1)->offset(0)->asArray()->all();
        return $List;
    }
    
    
    public static function getListone1($id){
        $Listimg =  (new Query())->select('a.*,b.img_url')->from("dh_commonmodel as a") ->leftJoin("dh_base_img as b","a.img_id=b.id")->Where(['a.Status'=>99,'a.NodeID'=>$id])->orderBy('a.stick DESC,(case  when  a.Priority IS NULL  then 99999999 else a.Priority end),a.Priority ASC,a.UpdateTime DESC')->limit(1)->offset(0)->all();
//	print_r($Listimg);die();
        return $Listimg;
    }
    
    
    public static function getImages($id){
        $Listimg =  (new Query())->select('a.*,b.img_url')->from("dh_commonmodel as a") ->leftJoin("dh_base_img as b","a.img_id=b.id")->Where(['a.Status'=>99,'a.NodeID'=>$id])->orderBy('a.stick DESC,(case  when  a.Priority IS NULL  then 99999999 else a.Priority end),a.Priority ASC,a.UpdateTime DESC')->all();
//	print_r($Listimg);die();
        return $Listimg;
    }
    
    public static function getImg($id){
        $condition = ' and a.img_id !="" and a.Status=99';
        $Listimg =  (new Query())->select('a.*,b.img_url')->from("dh_commonmodel as a") ->leftJoin("dh_base_img as b","a.img_id=b.id")->Where(['a.NodeID'=>$id])->andWhere('1=1'.$condition)->orderBy("a.stick DESC,(case  when  a.Priority IS NULL  then 99999999 else a.Priority end),a.Priority ASC,a.UpdateTime DESC")->limit(4)->offset(0)->all();
        return $Listimg;
    }
    
    public static function getOnline($id){
        $condition = ' and a.img_id !="" and a.Status=99';
        $Listimg =  (new Query())->select('a.*,b.img_url,c.Content')->from("dh_commonmodel as a") ->
                leftJoin("dh_base_img as b","a.img_id=b.id")->leftJoin("dh_article as c","a.GeneralID=c.ID")->Where(['a.NodeID'=>$id])->andWhere('1=1'.$condition)->orderBy("a.stick DESC,(case  when  a.Priority IS NULL  then 99999999 else a.Priority end),a.Priority ASC,a.UpdateTime DESC")->one();
        return $Listimg;
    }
    
    public static function getOnline1($id){
        $condition = ' and Status=99';
        $List =  (new Query())->select('*')->from("dh_commonmodel")->Where(['NodeID'=>$id])->andWhere('1=1'.$condition)->orderBy('stick DESC,(case  when  Priority IS NULL  then 99999999 else Priority end),Priority ASC,UpdateTime DESC')->limit(2)->offset(1)->all();
        return $List;
    }
    
    public static function getOnline2($id){
        $condition = ' and Status=99';
        $List =  (new Query())->select('*')->from("dh_commonmodel")->Where(['NodeID'=>$id])->andWhere('1=1'.$condition)->orderBy('stick DESC,(case  when  Priority IS NULL  then 99999999 else Priority end),Priority ASC,UpdateTime DESC')->limit(2)->offset(3)->all();
        return $List;
    }
    
    public static function getZt($id){
        $condition = ' and a.img_id !="" and a.Status=99';
        $Listimg =  (new Query())->select('a.*,b.img_url')->from("dh_commonmodel as a") ->
                leftJoin("dh_base_img as b","a.img_id=b.id")->Where(['a.NodeID'=>$id])->andWhere('1=1'.$condition)->orderBy("a.stick DESC,(case  when  a.Priority IS NULL  then 99999999 else a.Priority end),a.Priority ASC,a.UpdateTime DESC")->one();
        return $Listimg;
    }
    
    
    public static function getChildList($NodeID){
        $List = (new Query())->select('*')->from("dh_commonmodel") ->where(['Status'=>99,'NodeID'=>$NodeID])->orderBy("stick DESC,(case  when  Priority IS NULL  then 99999999 else Priority end),Priority ASC,UpdateTime DESC")->limit(4)->offset(0)->all();
//        print_r($List);
        return $List;
    }
    
    public static function getParentNews($id){
        $Node = DhNodes::findOne(["NodeID"=>$id]);
//        print_r($Node->ParentID);die();
        return $Node->ParentID;
    }
}
